<template>
  <div class="digital-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="horizontal" :model="where">
          <a-row :gutter="8">
            <a-col :span="6">
              <a-form-item name="modelName" label="模型名称">
                <a-input
                  v-model:value.trim="where.modelName"
                  placeholder="请输入模型名称"
                  allow-clear
                />
              </a-form-item>
              <a-form-item name="classify" label="资产分类">
                <a-select v-model:value="where.classify" placeholder="请选择资产分类">
                  <a-select-option value=''>全部分类</a-select-option>
                  <a-select-option
                    v-for="item in options.classify"
                    :key="item.value"
                    :value="item.value"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item name="author" label="作者姓名">
                <a-input
                  v-model:value.trim="where.author"
                  placeholder="请输入作者"
                  allow-clear
                />
              </a-form-item>
              <a-form-item label="开始时间">
                <a-date-picker
                  v-model:value="where.startTime"
                  style="width: 100%"
                  value-format="YYYY-MM-DD"
                  class="ele-fluid"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item name="dept" label="所属部门">
                <a-input v-model:value.trim="where.dept" placeholder="请输入部门" allow-clear />
              </a-form-item>
              <a-form-item label="结束时间">
                <a-date-picker
                  v-model:value="where.endTime"
                  style="width: 100%"
                  value-format="YYYY-MM-DD"
                  class="ele-fluid"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item class="ele-text-center">
                <a-space>
                  <a-button type="primary" @click="reload">查询</a-button>
                  <a-button @click="reset">重置</a-button>
                </a-space>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false" class="table-height">
        <BasicTable
          :canResize="false"
          ref="tableRef"
          :api="ModelMgmtApi.findPage"
          :where="where"
          :columns="columns"
          showTableSetting
          rowKey="id"
          :rowSelection="{
            type: 'checkbox',
            selectedRowKeys: checkedKeys,
            onChange: onSelectChange,
          }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <div class="table-toolbar">
              <a-space>
                <a-button type="primary" @click="onAdd">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新增</span>
                </a-button>
              </a-space>
            </div>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- 图片预览 -->
            <template v-if="column.dataIndex === 'thumbnailPreview'">
              <a-image :width="60" :src="WEB_VIEWER_URL_BASE + '/assets/models/' + record.path + '/offscreen.png'" />
            </template>

            <template v-if="column.dataIndex == 'classify'">
              <a-tag color="green">{{options.getClassifyName(record.classify)}}</a-tag>
            </template>

            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="onHistory(record)">所有版本</a>
                <a-divider type="vertical" />
                <a @click="onNew(record.id)">上传新版本</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要复制此记录吗？" @confirm="onCopy(record)">
                  <a class="digital-text-danger">复制</a>
                </a-popconfirm>
              </a-space>
              <br />
              <a-space>
                <a @click="onPreview(record)">最新版预览</a>
                <a-divider type="vertical" />
                <a @click="onDetail(record)">最新版详情</a>

              </a-space>
            </template>
          </template>
        </BasicTable>
      </a-card>
    </div>

    <!-- 版本记录 -->
    <history-model-version v-model:visible="showEdit" :data="current" @done="reload" @close="reload" v-if="showEdit" />
    <!-- 新增 -->
    <add-model v-model:visible="showEdit2" :data="current" @done="reload" v-if="showEdit2" />
    <!-- 上传新版本 -->
    <new-model-version v-model:visible="showEdit3" :data="current" @done="reload" v-if="showEdit3" />
    <!-- 查看详情 -->
    <detail-model v-model:visible="showEdit4" :data="current" :path="path" v-if="showEdit4" />
    <!-- 预览 -->
    <new-version-preview v-model:visible="showEdit5" :data="current" @done="reload" v-if="showEdit5" />
  </div>
</template>

<script lang="ts" setup>
  import { WEB_VIEWER_URL_BASE } from '/@/config/settings';
  import { reactive, ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { ModelMgmtApi } from '/@/api/datadigital/modelMgmt/ModelMgmtApi';
  import HistoryModelVersion from './history/index.vue';
  import AddModel from './add/index.vue';
  import NewModelVersion from './new/index.vue';
  import DetailModel from './detail/index.vue';
  import NewVersionPreview from '../preview/view/index.vue';
  import { message } from 'ant-design-vue';
  import * as options from '/@/utils/options';
  import { router } from '/@/router';
  // 查询条件
  const where = reactive({
    modelName: '',
    classify: '',
    author: '',
    dept: '',
    startTime: '',
    endTime: '',
  });
  // 多选选中列表
  const checkedKeys = ref<Array<string | number>>([]);
  // 当前行数据
  const current = ref<any>(null);
  // 当前行path
  const path = ref<any>(null);
  // 是否显示版本记录
  const showEdit = ref<boolean>(false);
  // 是否显示新增
  const showEdit2 = ref<boolean>(false);
  // 是否显示上传新版本
  const showEdit3 = ref<boolean>(false);
  // 查看详情
  const showEdit4 = ref<boolean>(false);
  // 预览
  const showEdit5 = ref<boolean>(false);
  // 表格配置
  const columns = ref<string[]>([
    {
      title: '模型编号',
      dataIndex: 'modelNo',
      width: 160,
    },
    {
      title: '模型名称',
      dataIndex: 'modelName',
      width: 160,
    },
    {
      title: '缩略图预览',
      dataIndex: 'thumbnailPreview',
      width: 180,
    },
    {
      title: '分类标签',
      dataIndex: 'classify',
      width: 80,
    },
    {
      title: '作者',
      dataIndex: 'author',
      width: 160,
    },
    {
      title: '部门',
      dataIndex: 'dept',
      width: 160,
    },
    {
      title: '上传时间',
      dataIndex: 'createTime',
      width: 160,
    },
    {
      title: '原始文件名',
      dataIndex: 'testFile',
      width: 160,
    },
    {
      title: '原始大小(入口文件)',
      dataIndex: 'fileSizeRet',
      width: 160,
    },
    {
      title: '轻量化大小',
      dataIndex: 'lightWeightSizeRet',
      width: 110,
    },
    {
      title: '文件格式',
      dataIndex: 'fileType',
      width: 80,
    },
    {
      title: '下载次数',
      dataIndex: 'downloadCount',
      width: 110,
    },
    {
      title: '操作',
      key: 'action',
      fixed: 'right',
      width: 235,
      align: 'left',
    },
  ]);

  // ref
  const tableRef = ref(null);

  const reload = () => {
    checkedKeys.value = [];
    tableRef.value.reload({ page: 1 });
  };

  const reset = () => {
    where.modelName = '';
    where.classify = '';
    where.author = '';
    where.dept = '';
    where.startTime = '';
    where.endTime = '';
    reload();
  };

  // 表格选中改变
  const onSelectChange = (selectedRowKeys: (string | number)[]) => {
    checkedKeys.value = selectedRowKeys;
  };

  /*const openEdit = (row: any) => {
    current.value = row;
    showEdit.value = true;
  };*/

  const onAdd = () => {
    // router.push({ path: `/model/manage/add` });
    current.value = '';
    showEdit2.value = true;
  };

  const onNew = (id: string) => {
    // router.push({ path: `/model/manage/new`, query: { id } });
    current.value = id;
    showEdit3.value = true;
  };

  const onDetail = (row: any) => {
    current.value = row.id;
    path.value = row.path;
    showEdit4.value = true;
  };

  const onCopy = async (item: any) => {
    try {
      const res = await ModelMgmtApi.duplicate({ id: item.id });
      if (res.success) {
        message.success('复制成功!');
        reload();
      } else {
        message.error(res.message);
      }
    } finally {
    }
  };

  const onHistory = async (row: any) => {
    // router.push({ path: '/modelMgmt/history', query: { id: row.id } });
    // window.location = '/modelMgmt/history?id=' + row.id;
    current.value = row;
    showEdit.value = true;
  };

  const onPreview = async (row: any) => {
    current.value = row.path;
    showEdit5.value = true;
  };
</script>
<style lang="less" scoped>
  :deep(.ant-card-body) {
    padding: 10px 10px 0px 10px !important;
  }

  .ant-form-item {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    color: rgba(0, 0, 0, 0.85);
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5715;
    list-style: none;
    font-feature-settings: tnum;
    margin-bottom: 10px;
    vertical-align: top;
  }
</style>
